package com.xunk.equipment.job;

import com.xunk.common.core.constant.DnakeConstants;
import com.xunk.common.core.enums.dnake.DnakeActionEnum;
import com.xunk.equipment.domain.EquipmentDO;
import com.xunk.equipment.mapper.EquipmentMapper;
import com.xunk.equipment.service.dnake.DnakePublishComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 *
 * @Author: wangTong
 * @Description: 获取网关下子设备信息记录到设备影子中
 * @Date: 2021/5/14 15:02
 * @Version: 1.0
 */
@Component
@EnableScheduling
@Slf4j
public class DnakeDeviceInfoQueryJob {

    @Autowired
    EquipmentMapper equipmentMapper;
    @Autowired
    DnakePublishComponent dnakePublishComponent;

    /**
     * 狄耐克网关子设备状态信息获取，每半小时获取一次
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void dnakeGetDeviceInfoConfirm() {
        List<EquipmentDO> devices = equipmentMapper.selectGatewayBySupplier(DnakeConstants.SUPPLIER);
        devices.parallelStream().forEach(device ->
                dnakePublishComponent
                        .simplePublish(DnakeActionEnum.READALLDEVSTATE.getCode(),device.getDeviceSn(), DnakeConstants.SYSTEM));
    }

    /**
     * 狄耐克网关子设备在线状态获取，每5分钟获取一次
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void dnakeGetDeviceOnlineConfirm() {
        List<EquipmentDO> devices = equipmentMapper.selectGatewayBySupplier(DnakeConstants.SUPPLIER);
        devices.parallelStream().forEach(device ->
                dnakePublishComponent
                        .simplePublish(DnakeActionEnum.READALLDEVSTATUS.getCode(),device.getDeviceSn(), DnakeConstants.ONLINE));
    }

}
